package com.xijia.wuyouwallet.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.AmountData;
import com.xijia.wuyouwallet.bean.CertStatusData;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/11 0011.
 */

public class AttestationSuccessActivity extends BaseActivity implements View.OnClickListener{

    private ImageView attestation_suc_back;
    private TextView attestation_suc_name,attestation_suc_pNum,attestation_suc_status;

    //合伙人红包
    private Dialog redpacketDialog;
    private ImageView awardPacket_close_img;
    private TextView awardPacket_money_tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attestation_success);
        initView();
    }
    private void initView(){
        attestation_suc_back = (ImageView) findViewById(R.id.attestation_suc_back);
        attestation_suc_back.setOnClickListener(this);
        attestation_suc_name = (TextView) findViewById(R.id.attestation_suc_name);
        attestation_suc_pNum = (TextView) findViewById(R.id.attestation_suc_pNum);
        attestation_suc_status = (TextView) findViewById(R.id.attestation_suc_status);
        requestData();
        requestPacketData();
    }

    private void initRedpacketDialog(){
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_award_redpacket,null);
        redpacketDialog = new Dialog(this, R.style.redPacketDialogStyle);
        awardPacket_close_img = (ImageView) view.findViewById(R.id.awardPacket_close_img);
        awardPacket_close_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                redpacketDialog.dismiss();
            }
        });
        awardPacket_money_tv = (TextView) view.findViewById(R.id.awardPacket_money_tv);
        redpacketDialog.setContentView(view);
        redpacketDialog.setCanceledOnTouchOutside(false);
        //redpacketDialog.show();
    }

    /**
     * 获取实名认证红包金额
     */
    private void requestPacketData(){
        Map<String,Object> map = new HashMap<>();
        map.put("token",MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        XUtil.Post(UrlConfig.GET_CERT_REDPACKET,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取实名认证奖励红包result="+result);
                AmountData data = JSONObject.parseObject(result,AmountData.class);
                if (data.getCode()==200){
                    initRedpacketDialog();
                    awardPacket_money_tv.setText(data.getData().getAmount());
                    redpacketDialog.show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取实名认证红包onError="+ex.toString());
            }
        });
    }

    private void requestData(){
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        showProgressDialog(true);
        XUtil.Post(UrlConfig.GET_ATTESTATION_INFO_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("获取实名认证result"+result);
                CertStatusData data = JSONObject.parseObject(result,CertStatusData.class);
                if (data.getCode()==200){
                    attestation_suc_name.setText(data.getData().getList().getUsercertification_name());
                    attestation_suc_pNum.setText(data.getData().getList().getUsercertification_card());
                    attestation_suc_status.setText("已认证");
                }else {
                    showToast(data.getMsg());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                //MyLogger.wLog().e("获取实名认证onError"+ex.toString());
            }
        });
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.attestation_suc_back:
                finish();
                break;
        }
    }
}
